package com.company;

import com.company.bean.CheckField;
import com.company.bean.CheckResult;
import com.company.service.CheckFormatService;
import com.company.service.impl.CheckFormatServiceImpl;
import com.company.utils.FileUtils;
import com.opencsv.CSVWriter;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
public class Main {

    public static void main(String[] args) throws Exception {
        CheckFormatService checkFormatService = new CheckFormatServiceImpl();
        CheckResult checkResult = new CheckResult();
        checkResult.setCorrectSoundRecordNum(0);
        checkResult.setSoundRecordNum(0);
        checkResult.setInCorrectRecordNum(0);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String fileCheckPath;
        System.out.println("请输入索引文件（文件以csv结尾）:");
        fileCheckPath = br.readLine();
        br.close();
        List<CheckField> cfs = FileUtils.readFile(fileCheckPath);
        checkResult.setFilePath(fileCheckPath);
        File file = new File(fileCheckPath);
        String fileName = file.getName().split("\\.")[0];
        CSVWriter csvWriter = new CSVWriter(new FileWriter(file.getParent() + "/" + fileName + "_check_report.csv"));
        for (int i = 0; i < cfs.size(); i++) {
            CheckField cf = cfs.get(i);
            String checkResultString = checkFormatService.checkField(i + 2, cf, checkResult);
            if(StringUtils.isNotBlank(checkResultString)){
                csvWriter.writeNext(checkResultString.split(","));
            }
        }
        checkResult.setAllRecordNum(cfs.size());
        System.out.println(checkResult.getAllRecordNum() + "," + checkResult.getInCorrectRecordNum()
                + "," + checkResult.getSoundRecordNum() + "," + checkResult.getCorrectSoundRecordNum());
        List<String[]> records = new ArrayList<>();
        records.add(new String[]{"索引记录总数：" + checkResult.getAllRecordNum() + "条"});
        records.add(new String[]{"校验通过索引总数：" + (checkResult.getAllRecordNum() - checkResult.getInCorrectRecordNum()) + "条"});
        records.add(new String[]{"索引录音记录总数：" + checkResult.getSoundRecordNum() + "条"});
        records.add(new String[]{"校验通过录音记录总数：" + checkResult.getCorrectSoundRecordNum() + "条"});
        csvWriter.writeAll(records);
        csvWriter.flush();
        csvWriter.close();
    }
}
